/*
 * UserNet  --  TCP/IP implementation in userspace
 * Copyright (C) 2010  Robert Millan
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#define _GNU_SOURCE 1

#include <stdio.h>
#include <dlfcn.h>
#include <sys/socket.h>

static int (*real_socket)();
static int (*real_bind)();
static int (*real_listen)();
static int (*real_accept)();

int
socket (int namespace, int style, int protocol)
{
  if (namespace == AF_INET)
    {
      printf ("AF_INET socket is called\n");
      return -1;
    }
  else
    {
      if (! real_socket)
	real_socket = (int (*)()) dlsym (RTLD_NEXT, "socket");

      return real_socket (namespace, style, protocol);
    }
}

int
bind (int sockfd, const struct sockaddr *addr, socklen_t addrlen)
{
  if (! real_bind)
    real_bind = (int (*)()) dlsym (RTLD_NEXT, "bind");
  return bind (sockfd, addr, addrlen);
}

int
listen (int sockfd, int backlog)
{
  if (! real_listen)
    real_listen = (int (*)()) dlsym (RTLD_NEXT, "listen");
  return listen (sockfd, backlog);
}

int
accept (int sockfd, struct sockaddr *addr, socklen_t *addrlen)
{
  if (! real_accept)
    real_accept = (int (*)()) dlsym (RTLD_NEXT, "accept");
  return accept (sockfd, addr, addrlen);
}
